/**************************************************************
 *@ProjectName: blweibo
 *@FileName: CommentMessageListItemView.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiweibo.models.Comment;
import com.zhiweibo.models.User;
import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.TopicUtil;

/**
 * The Class CommentMessageListItemView.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class CommentMessageListItemView extends LinearLayout {

    /** The tv name. */
    private TextView tvName;

    /** The tv date. */
    private TextView tvDate;

    /** The m tv content. */
    private TextView mTvContent;

    /** The tv sub content. */
    private TextView tvSubContent;

    /** The m comment. */
    private Comment mComment;

    /** The iv item portrait. */
    private ImageView ivItemPortrait;

    /** The sub layout. */
    private LinearLayout subLayout;

    /** The tweet_upload_pic2. */
    private ImageView tweet_upload_pic2;

    /** The task1. */
    private AsyncTask task1;

    /** The task2. */
    private AsyncTask task2;

    /** The iv v. */
    private ImageView ivV;

    /** The iv hr. */
    private ImageView ivHR;

    /** The iv li. */
    private ImageView ivLi;

    /** The iv qi. */
    private ImageView ivQi;

    /**
     * Instantiates a new comment message list item view.
     * 
     * @param context
     *            the context
     * @param cm
     *            the cm
     */
    public CommentMessageListItemView(Context context, Comment cm) {
        super(context);
        mComment = cm;
        ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.itemview, this);
        tvName = (TextView) findViewById(R.id.tvItemName);
        tvDate = (TextView) findViewById(R.id.tvItemDate);
        tvSubContent = (TextView) findViewById(R.id.tvItemSubContent);
        mTvContent = (TextView) findViewById(R.id.tvItemContent);
        ivItemPortrait = (ImageView) findViewById(R.id.ivItemPortrait);
        subLayout = (LinearLayout) findViewById(R.id.subLayout);
        subLayout.setVisibility(View.VISIBLE);
        tweet_upload_pic2 = (ImageView) findViewById(R.id.tweet_upload_pic2);
        ivV = (ImageView) findViewById(R.id.ivItemV);
        ivHR = (ImageView) findViewById(R.id.ivItemHR);
        ivLi = (ImageView) findViewById(R.id.ivItemLi);
        ivQi = (ImageView) findViewById(R.id.ivItemQi);
        setUI();
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        tvName.setText(mComment.getUser().getUsername());
        TextPaint tp = tvName.getPaint();
        tp.setFakeBoldText(true);
        tvDate.setText(mComment.getDateline());
        int iSize = SettingsPref.getTextSize(getContext());
        mTvContent.setTextSize(iSize);
        tvSubContent.setTextSize(iSize);
        mTvContent.setText(Html.fromHtml(mComment.getText()), TextView.BufferType.SPANNABLE);
        User user = mComment.getUser();
        if (user.getV()) {
            ivV.setVisibility(View.VISIBLE);
        }
        if (user.getHR()) {
            ivHR.setVisibility(View.VISIBLE);
        }
        if (user.getL()) {
            ivLi.setVisibility(View.VISIBLE);
        }
        if (user.getQ()) {
            ivQi.setVisibility(View.VISIBLE);
        }
        task1 = new FetchImage(ivItemPortrait, mComment.getUser().getFace()).execute(0);
        TopicUtil.highlightContent(getContext(), (Spannable) mTvContent.getText(), false);
        if (!TextUtils.isEmpty(mComment.getRecomment().getText())) {
            tvSubContent.setText(
                    "@" + mComment.getRecomment().getUser().getUsername() + ":"
                            + Html.fromHtml(mComment.getRecomment().getText()), TextView.BufferType.SPANNABLE);
            TopicUtil.highlightContent(getContext(), (Spannable) tvSubContent.getText(), false);
        } else {
            if (!TextUtils.isEmpty(mComment.getTopic().getTid())) {
                tvSubContent.setText("@" + mComment.getTopic().getUser().getUsername() + ":"
                        + mComment.getTopic().getHText(), TextView.BufferType.SPANNABLE);
                TopicUtil.highlightContent(getContext(), (Spannable) tvSubContent.getText(), false);
            } else {
                tvSubContent.setText(getContext().getString(R.string.mblog_del));
            }
            if (!TextUtils.isEmpty(mComment.getTopic().getPic_s())) {
                tweet_upload_pic2.setVisibility(View.VISIBLE);
                task2 = new FetchImage(tweet_upload_pic2, mComment.getTopic().getPic_s()).execute(0);
            } else {
                tweet_upload_pic2.setVisibility(View.GONE);
            }
        }
    }

    /**
     * Update.
     * 
     * @param context
     *            the context
     * @param cm
     *            the cm
     */
    public void update(Context context, Comment cm) {
        mComment = cm;
        tweet_upload_pic2.setVisibility(View.GONE);
        tweet_upload_pic2.setImageResource(R.drawable.loading);
        ivV.setVisibility(View.GONE);
        ivHR.setVisibility(View.GONE);
        ivLi.setVisibility(View.GONE);
        ivQi.setVisibility(View.GONE);
        ivItemPortrait.setImageResource(R.drawable.portrait);
        if (task1 != null && task1.getStatus() == AsyncTask.Status.RUNNING) {
            task1.cancel(true);
        }
        if (task2 != null && task2.getStatus() == AsyncTask.Status.RUNNING) {
            task2.cancel(true);
        }
        setUI();
    }

    /**
     * The Class FetchImage.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchImage extends AsyncTask {

        /** The piv. */
        private ImageView piv;

        /** The str url. */
        private String strUrl;

        /** The bm. */
        private Bitmap bm;

        /**
         * Instantiates a new fetch image.
         * 
         * @param iv
         *            the iv
         * @param url
         *            the url
         */
        public FetchImage(ImageView iv, String url) {
            piv = iv;
            strUrl = url;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            bm = HttpUtil.getImage(getContext(), strUrl);
            return bm;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            piv.setImageBitmap(bm);
        }
    }
}
